/*----------------------------------------------------------------------------
 * Tencent is pleased to support the open source community by making TencentOS
 * available.
 *
 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
 * If you have downloaded a copy of the TencentOS binary from Tencent, please
 * note that the TencentOS binary is licensed under the BSD 3-Clause License.
 *
 * If you have downloaded a copy of the TencentOS source code from Tencent,
 * please note that TencentOS source code is licensed under the BSD 3-Clause
 * License, except for the third-party components listed below which are
 * subject to different license terms. Your integration of TencentOS into your
 * own projects may require compliance with the BSD 3-Clause License, as well
 * as the other licenses applicable to the third-party components included
 * within TencentOS.
 *---------------------------------------------------------------------------*/

#include "tos_hal.h"
/*----------------------------------------------------------------------------
 * Tencent is pleased to support the open source community by making TencentOS
 * available.
 *
 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
 * If you have downloaded a copy of the TencentOS binary from Tencent, please
 * note that the TencentOS binary is licensed under the BSD 3-Clause License.
 *
 * If you have downloaded a copy of the TencentOS source code from Tencent,
 * please note that TencentOS source code is licensed under the BSD 3-Clause
 * License, except for the third-party components listed below which are
 * subject to different license terms. Your integration of TencentOS into your
 * own projects may require compliance with the BSD 3-Clause License, as well
 * as the other licenses applicable to the third-party components included
 * within TencentOS.
 *---------------------------------------------------------------------------*/

#include "tos_hal.h"
#include "gd32w51x.h"
#include "gd32w51x_libopt.h"
#include "systick.h"

#define ESP8266_RST_Pin_Periph_Clock()   rcu_periph_clock_enable(RCU_GPIOC)       //PC时钟
#define ESP8266_RST_Pin_Port              GPIOC    //复位 
#define ESP8266_RST_Pin                   GPIO_PIN_13    //复位管脚
#define ESP8266_RST_Pin_SetH     gpio_bit_set(ESP8266_RST_Pin_Port, ESP8266_RST_Pin)
#define ESP8266_RST_Pin_SetL     gpio_bit_reset(ESP8266_RST_Pin_Port, ESP8266_RST_Pin)

//复位重启
void ESP8266_Rst(void)
{
    ESP8266_RST_Pin_SetL;
	delay_1ms(500);
    ESP8266_RST_Pin_SetH;
}

void ESP8266_Init(void)
{
    ESP8266_RST_Pin_Periph_Clock();
	
	gpio_mode_set(ESP8266_RST_Pin_Port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, ESP8266_RST_Pin);//PC13配置成输出
    gpio_output_options_set(ESP8266_RST_Pin_Port, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, ESP8266_RST_Pin);//PC13配置成推挽输出，25M速度
	ESP8266_Rst();
}

//USART1初始化，使用PC6(TX),PC7(RX)脚，115200波特率，无校验，8位数据，1位停止
static void gd_eval_com_init(void)
{
	/* enable GPIO clock */
	rcu_periph_clock_enable(RCU_GPIOC);//使能GPIOC时钟

	/* enable USART clock */
	rcu_periph_clock_enable(RCU_USART1);//使能USART1时钟
	//gpio_pin_remap_config(GPIO_USART0_REMAP, ENABLE);//PB6,PB7需要重映射

	gpio_af_set(GPIOC,GPIO_AF_8,GPIO_PIN_6);//pc6作为USRAT1需要根据芯片手册，配置AF_8
	gpio_af_set(GPIOC,GPIO_AF_8,GPIO_PIN_7);//pc7作为USRAT1需要根据芯片手册，配置AF_8
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);//PC6配置成输出
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO_PIN_6);//PC6配置成推挽输出，25M速度
	
	/* configure USART Rx as alternate function push-pull */

	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);//PC7配置成输入
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_25MHZ, GPIO_PIN_7);//PC7


	/* USART configure */
	usart_deinit(USART1);
	usart_baudrate_set(USART1,115200);
	usart_word_length_set(USART1, USART_WL_8BIT);
	usart_stop_bit_set(USART1, USART_STB_1BIT);
	usart_parity_config(USART1, USART_PM_NONE);

	usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);
	usart_receive_config(USART1, USART_RECEIVE_ENABLE);
	usart_enable(USART1);//使能USART1
}
__API__ int tos_hal_uart_init(hal_uart_t *uart, hal_uart_port_t port)
{
	if (!uart) {
        return -1;
    }
		/* enable GPIO clock */


    if (port == HAL_UART_PORT_0) {
		uart->private_uart = (void*)USART0;
    } else if (port == HAL_UART_PORT_1) {
		uart->private_uart = (void*)USART1;
    } else if (port == HAL_UART_PORT_2) {
        uart->private_uart = (void*)USART2;
    } 

	// gd_eval_com_init((uint32_t)uart->private_uart);
	// nvic_irq_enable(USART1_IRQn, 0, 0);//使能USART1中断
	// usart_interrupt_enable((uint32_t)uart->private_uart, USART_INT_RBNE);//接收中断打开
	// ESP8266_Init();
	rcu_periph_clock_enable(RCU_GPIOB);//使能GPIOB时钟
    rcu_periph_clock_enable(RCU_GPIOC); //使能GPIOC时钟
    gd_eval_com_init();
    nvic_irq_enable(USART1_IRQn, 0, 0);//使能USART1中断
    usart_interrupt_enable(USART1, USART_INT_RBNE);//接收中断打开
    ESP8266_Init();
	return 0;
}

__API__ int tos_hal_uart_write(hal_uart_t *uart, const uint8_t *buf, size_t size, uint32_t timeout)
{
	int i = 0;
	uint32_t uart_port = (uint32_t)uart->private_uart;
	for(i = 0; i < size; i++){
		usart_data_transmit(uart_port, buf[i]);
		while(usart_flag_get(uart_port, USART_FLAG_TC) == RESET);
		delay_1ms(10);
	}
	return 0;

}

__API__ int tos_hal_uart_read(hal_uart_t *uart, const uint8_t *buf, size_t size, uint32_t timeout)
{
	return 0;
}

__API__ int tos_hal_uart_deinit(hal_uart_t *uart)
{
	return 0;
}



